import { runAsWorker } from "synckit";

export type ResolvePrettierOptionsFn = (onDiskFilepath?: string) => {
  prettierFileInfo: import("prettier").FileInfoResult;
  prettierOptions: import("prettier").Options;
};

const vuePrettierOptions: import("prettier").Options = {
  tabWidth: 2,
  useTabs: false,
  parser: "vue",
  endOfLine: "auto",

  bracketSameLine: true,
  jsxBracketSameLine: true,
  singleAttributePerLine: true,
  vueIndentScriptAndStyle: true,
  htmlWhitespaceSensitivity: "ignore",

  arrowParens: "avoid",
  trailingComma: "es5",
  singleQuote: false,
  printWidth: 150,
};

let prettier: typeof import("prettier");

runAsWorker(async (onDiskFilepath: string) => {
  if (!prettier) {
    prettier = await import("prettier");
  }
  // 获取 .prettierrc.js 配置
  const prettierRcOptions = await (function () {
    return prettier.resolveConfig(onDiskFilepath, {
      editorconfig: true,
    });
  })();

  const prettierFileInfo = await prettier.getFileInfo(onDiskFilepath, {
    resolveConfig: false,
    withNodeModules: false,
    ignorePath: ".prettierignore",
    plugins: prettierRcOptions ? (prettierRcOptions.plugins as string[]) : void 0,
  });

  const prettierOptions = { ...vuePrettierOptions, ...prettierRcOptions };

  // Skip if file is ignored using a .prettierignore file
  return { prettierOptions: prettierOptions, prettierFileInfo };
});
